var express = require('express');
var router = express.Router();
var { getDecrypt } = require('../utils/index')
var monent = require('moment')
var {Mvs} = require('../utils/model')

/* GET home page. */
router.get('/', function (req, res, next) {
  console.log(req.session);
  res.render('index', {
    title: '电影评论管理系统',
    msg: 'daydayup-----',
    list: ['asd', 'asd', 'asdqw'],
    username: req.session.username,
    logintime: monent(req.session.logintime).format('llll'),
  });
});

router.get('/login', (req, res) => {
  //给用户名进行加密
  var username = req.query.username ? getDecrypt(req.query.username) : '';
  res.render('login', { username })
})

router.get('/register', (req, res) => {
  res.render('register')
})

//退出登录
router.get('/logout', (req, res) => {
  //destroy  清空，清空所有登录信息
  req.session.destroy(() => {
    //重定向 ，回到主页
    res.redirect('/')
  })
})

//修改密码

router.get('/changepass', (req, res) => {
  if (req.session.username) {
    res.render('changepass', {
      username: req.session.username
    })
  } else {
    res.send(`<script>alert('你的session已经过期无效，请重新登录');location.href='/login'</script>`)
  }

})


//电影列表
router.get('/movie', (req, res) => {
  if (req.session.username) {
    //查找升序降序传过来的query
    var query = req.query;
    var sortObj = {
      _id:1
    }
    //查找搜索传过来的query
    var keyword = query.keyword;
    var obj = {}

    //如果搜索就有keyword   ，没有搜索就排序
    //new RegExp()  正则查找
    if(keyword){
      obj = {
        $or:[
          {title:new RegExp(keyword)},
          {year:new RegExp(keyword)},
          {genres:new RegExp(keyword)},
          {'rating.average':new RegExp(keyword)},
          {'directors.name':new RegExp(keyword)}
        ]
      }
    }else{
      sortObj = {...query,...sortObj} //对象合并   
    }
    
    
    Mvs.find(obj,{
      _id:0
    }).sort(sortObj)
    .then(result=>{
      console.log(result);
      res.render('movie', {
        
        username: req.session.username,
        result,     //电影数据
      })
    })
   
  } else {
    res.send(`<script>alert('你的session已经过期无效，请重新登录');location.href='/login'</script>`)
  }

})



// router.get('/talk',(req,res)=>{
//   var query = req.query;
//   var body = req.body;
//   // res.send('1231')
//   res.render('talk',{username:'12316'})
// })














router.get('/demo', (req, res) => {
  res.send('demo---demo---demo')
})

router.get('/test', (req, res) => {
  console.log(req.url);
  res.send('test---test---test')
})

router.get('/findOne', (req, res) => {
  res.json({
    code: 200,
    msg: '查询成功',
    url: req.url,
    pathname: req.path,
    query: req.query,    //查询参数
    headers: req.headers,   //请求头

  })
})

//凡是：冒号声明的参数只能通过 params 取，？表明这个参数可以传可以不传
router.get('/delone/:uid?', (req, res) => {
  res.json({
    code: 200,
    msg: '删除成功',
    params: req.params   // 路由参数  
  })
})

router.all("/all", (req, res) => {
  res.send('这个接口是get&post请求方式..')
})

router.post("/addone", (req, res) => {
  res.json({
    msg: '添加成功',
    code: 200,
    method: 'POST',
    body: req.body   // post 请求提交的data数据用body
  })
})

module.exports = router;
